// 获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    filter: {
      onlyBacklog:false,
      typeId:0,
      Types: [{
          text: '类型',
          value: 0
        },
        {
          text: '日巡检',
          value: 1
        },
        {
          text: '周巡检',
          value: 2
        },
        {
          text: '月巡检',
          value: 3
        },
      ],
      name: '',
    },
    pageIndex: 0, // 当前页Index
    pageSize: 20,
    totalPages: 1, // 明细页数
  },

  loadList: function (push = false) {
    const _this = this;
    const {
      filter: {
        name,
        typeId,
        onlyBacklog
      },
      lists,
      pageIndex: i,
      pageSize,
      totalPages: t
    } = _this.data;
    let _lists = lists;
    if (push && i + 1 >= t) return;
    app.ajax({
      url: 'InspectionPlan/List',
      data: {
        name,
        typeId,
        onlyBacklog,
        pageIndex: push ? i + 1 : i,
        pageSize
      },
      verifyToken: true,
      success(data) {
        if (data.result) {
          const {
            model = [], PageIndex = 0, TotalPages = 1
          } = data;
          _lists = push ? [..._lists, ...model || []] : model || [];
          _this.setData({
            lists: _lists,
            pageIndex: PageIndex,
            totalPages: TotalPages
          });
        } else {
          app.showNotify(data.msg);
          return;
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadList(true);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  stop() {
    console.log("进入后台");
  },

  done(e) {
    console.log("初始化完成", e);
  },

  error(e) {
    app.showNotify('用户未授权');
  },

  onTypeChange({
    detail
  }) {
    const _this = this;
    const {
      filter
    } = _this.data;
    filter.typeId = detail;
    _this.setData({
      filter,
      pageIndex: 0,
      totalPages: 1
    });
    _this.loadList();
  },

  // 根据筛选条件查询
  onSearch() {
    const _this = this;
    _this.selectComponent('#item').toggle();
    _this.setData({
      pageIndex: 0,
      totalPages: 1
    });
    _this.loadList();
  },

  // 输入框值变化
  onFieldChange(e) {
    const _this = this;
    const {
      filter
    } = _this.data;
    const {
      detail,
      target: {
        id
      }
    } = e;
    filter[id] = detail;
    this.setData({
      filter
    });
  },

  // 输入框值变化
  onSwitchChange(e) {
    const _this = this;
    const {
      filter
    } = _this.data;
    const {
      detail,
      target: {
        id
      }
    } = e;
    filter[id] = detail;
    this.setData({
      filter
    });
  },
})